﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace CarInfor.UI
{
    public partial class frmMsgSettings : Form
    {

        XmlDocument xmlDoc;
        string filename;
        public frmMsgSettings()
        {
            InitializeComponent();
            Init();
        }


        private void Init()
        {
            filename = System.Windows.Forms.Application.StartupPath + "\\Config.xml";
            xmlDoc = new XmlDocument();
            // 判断文件是否存在
            if (!File.Exists(filename))
            {
                xmlDoc = CreateXmlFile(filename);
            }
            else
            {
                xmlDoc.Load(filename);
            }
            XmlNode node = xmlDoc.SelectSingleNode("Setting/MsgPath");
            if (node != null)
            {
                this.txtPath.Text = node.InnerText;
            }

            XmlNode nodePageSize = xmlDoc.SelectSingleNode("Setting/PageSize");
            if (nodePageSize != null)
            {
                this.comboBox1.SelectedItem = nodePageSize.InnerText;
            }
            else
            {
                this.comboBox1.SelectedIndex = 0;
            }

            this.txtPath.SelectionStart = this.txtPath.Text.Length;
        }


        private void btnOK_Click(object sender, EventArgs e)
        {
            XmlNode node = xmlDoc.SelectSingleNode("Setting/MsgPath");
            node.InnerText = this.txtPath.Text.Trim();

            XmlNode nodepageSize = xmlDoc.SelectSingleNode("Setting/PageSize");
            nodepageSize.InnerText = this.comboBox1.SelectedItem.ToString();
            xmlDoc.Save(filename);
            this.DialogResult = DialogResult.OK;
        }



        //创建Xml文件
        private XmlDocument CreateXmlFile(string filename)
        {
            xmlDoc = new XmlDocument();
            XmlNode root;
            XmlDeclaration declare = xmlDoc.CreateXmlDeclaration("1.0", "gb2312", null);
            xmlDoc.AppendChild(declare);
            root = xmlDoc.CreateElement("Setting");
            XmlNode newnode = xmlDoc.CreateElement("MsgPath");
            newnode.InnerText = "";
            root.AppendChild(newnode);
            xmlDoc.AppendChild(root);
            xmlDoc.Save(filename);
            return xmlDoc;
        }
        private void frmMsgSettings_Load(object sender, EventArgs e)
        {


        }
    }
}
